/**
 * @filename:ShebeiController Dec 21, 2023
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wonder.app.domain.Chanpin;
import com.wonder.app.domain.Shebei;
import com.wonder.app.mapper.ShebeiDao;
import com.wonder.app.service.ShebeiService;
import com.wonder.app.request.ShebeiRequest;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.web3j.protocol.admin.Admin;

/**
 * 
 * <p>说明： 设备信息API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Dec 21, 2023
 *
 */
@Api(tags = "设备信息")
@RestController
@RequestMapping("/app/Shebei")
public class ShebeiController {
	
	@Autowired
	private ShebeiService shebeiService;
	@Autowired
	private ShebeiDao shebeiDao;
	@Autowired
	private CheckAdminToken checkAdminToken;
	@Autowired
	private SysAdminService adminService;

	/**
	 * @userName 登录用户名称
	 * @user_id   登录用户id
	 */

	@ApiOperation(value = "设备信息列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody ShebeiRequest shebeiRequest) {
		Page<Shebei> page = new Page<Shebei>(shebeiRequest.getPage(), shebeiRequest.getLimit());
		IPage<Shebei> list = shebeiService.selectList(shebeiRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));


		return AjaxMap.success(retMap);
	}

	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Shebei> updateExt(@RequestBody Shebei entity){
		if (null!=entity) {
			boolean rsg = shebeiService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Shebei> add(HttpServletRequest request , @RequestBody Shebei entity) throws Exception {

		if (null!=entity) {
		    
		    String validate=shebeiService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);

            //获取token
            String token = checkAdminToken.getTokenFormRequest(request);
            SysAdmin user = adminService.getAdminByToken(token);
            //获取登录名 并赋值
            String userName = user.getName();
            entity.setCreatorId(userName);

            //给提交和审核赋值初始值
			entity.setTijiao(false);
			entity.setShenhe(false);

            //获取当前日期
			Date currentDate = new Date();
			entity.setCreatedate(currentDate);//创建日期

			boolean rsg = shebeiService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取设备信息对象", notes = "作者：Wonder")
	public AjaxResult<Shebei> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long fid=Long.parseLong(map.get("fid").toString());		
		 Shebei shebei=shebeiService.getById(fid);
		
		if (null!=shebei ) {
			 return AjaxResult.success(shebei);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Shebei> update(HttpServletRequest request , @RequestBody Shebei entity) throws Exception {
		if (null!=entity) {
		    String validate=shebeiService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);

            //获取token
            String token = checkAdminToken.getTokenFormRequest(request);
            SysAdmin user = adminService.getAdminByToken(token);
            //获取登录名 并赋值
            String userName = user.getName();
            entity.setModifierId(userName);

            //获取当前日期
            Date currentDate = new Date();
			entity.setModifydate(currentDate);//修改日期
            //获取提交状态
            boolean tj = entity.getTijiao();

			boolean rsg ;
            //当数据未提交成功时执行修改操作
			if(tj != true){
				rsg = shebeiService.updateById(entity);
			}else{
				rsg = false;
			}
			if (rsg) {
				return AjaxResult.success("修改成功");
			} else {
				return AjaxResult.failed("已经提交或审核");
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}


	/**
	 * @explain 提交
	 */
	@PostMapping("/shebei_tijiao")
	@ApiOperation(value = "修改提交状态", notes = "作者：Wonder")
	public AjaxResult<Boolean> isTiJiao(HttpServletRequest request , @RequestBody Map<String,Object> map) throws Exception {
		Long fid = Long.parseLong(map.get("fid").toString());

		boolean res =  shebeiService.shebeiTiJiao(fid);
		if (res){
			return AjaxResult.success("提交成功");
		} else {
			return AjaxResult.failed("已经提交或审核过了");
		}
	}


	/**
	 * @explain 撤销
	 */
	@PostMapping("/shebei_chexiao")
	@ApiOperation(value = "修改提交状态", notes = "作者：Wonder")
	public AjaxResult<Boolean> isChexiao(@RequestBody Map<String,Object> map){
		Long fid = Long.parseLong(map.get("fid").toString());
		boolean res =  shebeiService.shebeiCheXiao(fid);
		if (res){
			return AjaxResult.success("撤销成功");
		} else {
			return AjaxResult.failed("数据已经审核或数据未提交");
		}
	}


	/**
	 * @explain 审核
	 */
	@PostMapping("/shebei_shenhe")
	@ApiOperation(value = "修改审核状态", notes = "作者：Wonder")
	public AjaxResult<Boolean> isShenhe(HttpServletRequest request , @RequestBody Map<String,Object> map) throws Exception {
		Long fid = Long.parseLong(map.get("fid").toString());

		Shebei shebei = shebeiDao.selectById(fid);
		//获取token
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		//获取登录名 并赋值
		String userName = user.getName();
		shebei.setAuditorId(userName);
		shebeiService.updateById(shebei);
		boolean res =  shebeiService.shebeiShenHe(fid);
		if (res){
			return AjaxResult.success("审核成功");
		} else {
			return AjaxResult.failed("还未提交或者已经审核");
		}
	}


	/**
	 * @explain 反审核
	 */
	@PostMapping("/shebei_fanshenhe")
	@ApiOperation(value = "修改审核状态", notes = "作者：Wonder")
	public AjaxResult<Boolean> isFanshenhe(@RequestBody Map<String,Object> map){
		Long fid = Long.parseLong(map.get("fid").toString());
		boolean res =  shebeiService.shebeiFanShenHe(fid);
		if (res){
			return AjaxResult.success("反审核成功");
		} else {
			return AjaxResult.failed("数据未审核或数据未提交");
		}
	}
	
	
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Shebei> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("fid").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	   // boolean rsg = shebeiService.removeBatchByIds(idList);
		Shebei shebei = shebeiDao.selectById(ids);
		//获取提交和审核状态
		String tj = shebei.getTijiao().toString();
		boolean rsg ;
		if(tj != "true"){
			rsg = shebeiService.removeBatchByIds(idList);
		}else{
			rsg = false;
		}
		if (rsg) {
			return AjaxResult.success("删除成功");
		}else {
			return AjaxResult.failed("已审核或提交的数据不允许删除");
		}
//	    if (rsg) {
//		    return AjaxResult.success("system.deleteSuccess");
//	    }else {
//		   return AjaxResult.failed("system.deleteFailed");
//	    }
		
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取设备信息对象明细", notes = "作者：Wonder")
	public AjaxResult<Shebei> detail(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long fid=Long.parseLong(map.get("fid").toString());		
		Shebei shebei=shebeiService.getById(fid);
		if (null!=shebei ) {	
			 return AjaxResult.success(shebei);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	
}